### Alizade, Dancygier, Ditlmann
### "National Penalties Reversed"
### Replication Code 
### Table A1
### For questions, contact jalizade@princeton.edu

# setup
rm(list = ls())
setwd("C:/Users/Jey/Dropbox/WZB/NaturalizationExperiment/Submission/JOP/replication_JOP/data")
library(foreign)
dat <- read.dta("data_experimental.dta")
datmun <- read.dta("data_covars_mun.dta")

# filter first experiment
dat <- dat[dat$e1_treated==1,]
datmun <- datmun[datmun$e1_treated==1,]

# code FDP as not center-left for the purposes of this table
dat$e1_leftpol[dat$e1_treated==1 & is.na(dat$e1_leftpol)] <- 0

# function to calculate and display summary statistics
sumstats <- function(df, var) {
  data.frame(
    N=sum(!is.na(df[,var])), 
    Mean=mean(df[,var], na.rm=T), 
    SD=sd(df[,var], na.rm=T), 
    Min=min(df[,var], na.rm=T), 
    Max=max(df[,var], na.rm=T)
  ) 
}

# get summary statistics for individual-level variables
t(sapply(c("e1_response", "e1_fempol", "e1_leftpol", "e1_cfpol"), function(x) sumstats(dat, x)))

# get summary statistics for aggregate-level variables
t(sapply(c("pct_turk_pop", "pct_forcit_pop", "pop", "pop_density", "pct_welfare", "pct_coll_qual", "e1_n_council", "e1_pct_left", "e1_pct_bigpart"), 
         function(x) sumstats(datmun, x)))
